//@author A0096950U
#include "CurrentDisplay.h"

CurrentDisplay::CurrentDisplay(void) : logger(Logger::getInstance()) {
	setToTodayTask();

	logger.log(LOG_DEBUGGING, "CurrentDisplay: initialised.");
}

void CurrentDisplay::setToSearchResult() {
	mainDisplay = &searchResultDisplay;

	logger.log(LOG_DEBUGGING, "CurrentDisplay: set to searchResultDisplay.");
}

void CurrentDisplay::setToTodayTask() {
	mainDisplay = &todayTaskDisplay;

	logger.log(LOG_DEBUGGING, "CurrentDisplay: set to todayTaskDisplay.");
}

void CurrentDisplay::setCurrentDisplay(vector<Task>& newDisplay) {
	assert(mainDisplay != NULL);

	mainDisplay->clear();
	doneDisplay.clear();

	for(unsigned i = 0; i < newDisplay.size(); i++) {
		push_back(newDisplay[i]);
	}

	sortDisplay();
}

vector<Task> CurrentDisplay::getMainDisplay() const {
	assert(mainDisplay != NULL);

	return mainDisplay->getTasksDisplayed();
}

vector<Task> CurrentDisplay::getDoneDisplay() const {
	return doneDisplay.getTasksDisplayed();
}

int CurrentDisplay::getMainDisplaySize() const {
	assert(mainDisplay != NULL);

	return mainDisplay->size();
}

int CurrentDisplay::getDoneDisplaySize() const {
	return doneDisplay.size();
}

void CurrentDisplay::push_back(Task& taskToAdd) {
	assert(mainDisplay != NULL);

	if(taskToAdd.getStatus() == TASK_DONE) {
		doneDisplay.push_back(taskToAdd);
	}
	else {
		mainDisplay->push_back(taskToAdd);
	}
}

void CurrentDisplay::sortDisplay() {
	sortByTime();
	sortByDate();
}

void CurrentDisplay::sortByTime() {
	assert(mainDisplay != NULL);

	mainDisplay->sortByTime();
	doneDisplay.sortByTime();
}

void CurrentDisplay::sortByDate() {
	assert(mainDisplay != NULL);

	mainDisplay->sortByDate();
	doneDisplay.sortByDate();
}

void CurrentDisplay::remove(int userIndex) {
	assert(mainDisplay != NULL);

	mainDisplay->remove(userIndex);
}

void CurrentDisplay::remove(string taskID) {
	assert(mainDisplay != NULL);

	mainDisplay->remove(taskID);
}

void CurrentDisplay::clear() {
	assert(mainDisplay != NULL);

	mainDisplay->clear();
	doneDisplay.clear();
}

void CurrentDisplay::modify(Task& newTask) {
	assert(mainDisplay != NULL);

	mainDisplay->modify(newTask);
}

void CurrentDisplay::setToDone(int userIndex) {
	assert(mainDisplay != NULL);

	Task taskDone= mainDisplay->getTask(userIndex);
	doneDisplay.push_back(taskDone);

	mainDisplay->remove(userIndex);
}

void CurrentDisplay::undone(string taskID) {
	assert(mainDisplay != NULL);

	Task taskUndone = doneDisplay.getTask(taskID);
	mainDisplay->push_back(taskUndone);

	doneDisplay.remove(taskID);
}

Task CurrentDisplay::getMainDisplayTask(int userIndex) const {
	assert(mainDisplay != NULL);

	return mainDisplay->getTask(userIndex);
}

string CurrentDisplay::getFeedback() const {
	return feedback;
}

void CurrentDisplay::setFeedback(const string& newFeedback) {
	feedback = newFeedback;
}

void CurrentDisplay::setSearchKeyword(const string& keyword) {
	searchKeyword = keyword;
}

string CurrentDisplay::getSearchKeyword() const {
	return searchKeyword;
}

bool CurrentDisplay::isSearchResultDisplay() const {
	return (mainDisplay == &searchResultDisplay);
}

//@author A0105511Y
void CurrentDisplay::setToOngoing(int userIndex) {
	mainDisplay->setStatus(userIndex, TASK_ONGOING);
}

void CurrentDisplay::setToOverdue(int userIndex) {
	mainDisplay->setStatus(userIndex, TASK_OVERDUE);
}